package edu.hcnu.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @description:
 * @author: Administrator
 * @time: 2022/9/21 18:22
 */
@Aspect
@Component
public class LogUtil {
    /**
     * ("execution(* edu.hcnu.proxy.dynamic..*.*(..))")
     * * 代表所有的返回值类型
     * .. 代表子孙包
     * * 所有的类
     * * 类下的所有方法
     * .。 方法下的所有参数
     */

    @Pointcut("execution(* edu.hcnu.proxy.dynamic..*.*(..))")
    public void pointcut() {
    }


    // 前置通知
    @Before("pointcut()")
    public static void before(JoinPoint joinPoint) {

        String methodName = joinPoint.getSignature().getName();

        Object[] args = joinPoint.getArgs();


        System.out.println(methodName + "方法运行前，参数是" + ((args == null) ? "" : Arrays.asList(args)));
    }

    //后置通知
    @After("pointcut()")
    public static void after(JoinPoint joinPoint) {

        String methodName = joinPoint.getSignature().getName();

        Object[] args = joinPoint.getArgs();
        System.out.println(methodName + "方法运行后，参数是" + ((args == null) ? "" : Arrays.asList(args)));
    }

    //后置异常通知
    @AfterThrowing(value = "pointcut()", throwing = "ex")
    public static void afterException(JoinPoint joinPoint, Exception ex) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName + "方法出错了" + ex.getMessage());
    }

    //后置返回通知
    @AfterReturning(value = "pointcut()", returning = "result")
    public static void afterEnd(JoinPoint joinPoint, Object result) {
        String methodName = joinPoint.getSignature().getName();

        System.out.println(methodName + "方法结束 结果是：" + result);
    }
}
